﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MM.Common
{
    /// <summary>
    /// 扩展类
    /// </summary>
    public static class ExpandMethod
    {
        /// <summary>
        /// DataTable是否为Null或空
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 字符串是否为Null或空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="_str"></param>
        /// <param name="length">字符串长度</param>
        /// <returns></returns>
        public static string CutString(this string str, int length)
        {
            str = str.Length >= length ? str.Substring(0, length) + "..." : (str.IsNullOrEmpty() ? "" : str);
            return str;
        }


        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            return int.Parse(str);
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(this object obj)
        {
            return int.Parse(obj.ToString());
        }
        /// <summary>
        /// 转化为decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj)
        {
            return decimal.Parse(obj.ToString());
        }
        /// <summary>
        /// 转化为double
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(this object obj)
        {
            return double.Parse(obj.ToString());
        }

        /// <summary>
        /// 执行Action委托
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="action"></param>
        public static void DoMethod<T>(this T t, Action action)
        {
            action();
        }

        /// <summary>
        /// 执行Func委托
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="t"></param>
        /// <param name="action"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        public static K DoMethod<T, K>(this T t, Func<T, K> func)
        {
            return func(t);
        }

        /// <summary>
        /// 返回内容不重复的字符串
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="colname">列名</param>
        /// <param name="creator">发起人</param>
        /// <returns></returns>
        public static string DistinctDT(this DataTable dt, string colname, string creator)
        {
            string result = string.Empty;
            if (dt.Rows.Count > 0)
                result = dt.Rows[0][colname].ToString();
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                string[] row = dt.Rows[i][colname].ToString().Split(',');
                foreach (string str in row)
                {
                    if (!result.Contains(str))
                        result += "," + str;
                }
            }
            if (!result.Contains(creator))
                result += "," + creator;
            return result.TrimStart(',').TrimEnd(',');
        }
        /// <summary>
        /// 转换为DataTable
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(this DataRow[] rows)
        {
            DataTable dt = new DataTable();
            if (rows != null && rows.Count() > 0)
            {
                dt = rows[0].Table.Clone();
                dt.BeginInit();
                foreach (DataRow row in rows)
                {
                    dt.ImportRow(row);
                }
                dt.EndInit();
            }
            return dt;
        }

        /// <summary>
        /// 转换为Int，对象为空是默认为-1
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToIntOr(this object obj)
        {
            if (obj == null)
                return -1;
            else
                return int.Parse(obj.ToString());
        }

        /// <summary>
        /// 转换为decimal，对象为空是默认为-1
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimalOr(this object obj)
        {
            if (obj == null)
                return -1;
            else
                return decimal.Parse(obj.ToString());
        }

        /// <summary>
        /// 转换为string，对象为空是默认为空
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToStringOr(this object obj)
        {
            if (obj == null)
                return "";
            else
                return obj.ToString();
        }
        public static byte[] ToBytes(this string value, Encoding encoding)
        {
            int num = 1;
        Label_000A:
            switch (num)
            {
                case 0:
                    return null;

                case 2:
                    encoding = Encoding.Default;
                    return encoding.GetBytes(value);

                case 3:
                    if (((encoding != null) ? 0 : 1) != 0)
                    {
                    }
                    num = 2;
                    goto Label_000A;
            }
            if (value.IsNullEmpty())
            {
                num = 0;
            }
            else
            {
                num = 3;
            }
            goto Label_000A;
        }
        public static bool IsNullEmpty(this string str)
        {
            if (str != null)
            {
                return string.IsNullOrEmpty(str);
            }
            return true;
        }
        public static byte[] FormBase64(this string str)
        {
            return Convert.FromBase64String(str);
        }
    }
}
